﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FullThrustLogic.Core;
using FullThrustLogic.Core.GameState;
using FullThrustLogic.Helper;
using FullThrustLogic.ShipComponent;
using FullThrustLogic.ShipComponent.Weapon;
using LogicTest.Forms;

namespace LogicTest.Controls
{
    public partial class AssignMovement : UserControl
    {
        private GameContext _currentGame;
        private PlayerShips _playerShips;
        private MapControl _mapControl;

        private ContextMenu _treeContextMenu;
        private MenuItem _assignMovementOrder;
        private MenuItem _inspectShip;


        public AssignMovement(GameContext currentGame, MapControl mapControl)
        {
            InitializeComponent();
            _currentGame = currentGame;
            _mapControl = mapControl;

            
            _treeContextMenu = new ContextMenu();

            _assignMovementOrder = new MenuItem("Assign Movement Order");
            _inspectShip = new MenuItem("Inspect Ship");

            _treeContextMenu.MenuItems.Add(_assignMovementOrder);
            //_treeContextMenu.MenuItems.Add(_inspectShip);

            _assignMovementOrder.Click += new EventHandler(_assignMovementOrder_Click);

            

          
        }

        void _assignMovementOrder_Click(object sender, EventArgs e)
        {
            AssignMovementWizard frmAssignMovementWizard = new AssignMovementWizard(_playerShips.SelectedShip);
            frmAssignMovementWizard.Closed += new EventHandler(frmAssignMovementWizard_Closed);
            frmAssignMovementWizard.ShowDialog();
        }

        void frmAssignMovementWizard_Closed(object sender, EventArgs e)
        {
            _mapControl.UpdateMap(_playerShips.SelectedShip);
        }

        private void LoadContextMenuIntoShipTreeNodes(TreeNode ParentNode)
        {
            ParentNode.ToolTipText = "Right click ship to assign movement order";

            if (ParentNode.Tag is Ship)
            {
                ParentNode.ContextMenu = _treeContextMenu;
            }

            foreach (TreeNode node in ParentNode.Nodes)
            {
                LoadContextMenuIntoShipTreeNodes(node);
            }
        }

        private void cbCurrentPlayer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cbCurrentPlayer.SelectedItem != null
                && cbCurrentPlayer.SelectedItem is TypeOfPlayer)
            {
                pnlAvailableShips.Controls.Clear();
                _playerShips = new PlayerShips(_currentGame.Players[(int)((TypeOfPlayer)cbCurrentPlayer.SelectedItem)].Ships){Dock = DockStyle.Fill};
                pnlAvailableShips.Controls.Add(_playerShips);

                _playerShips.tvPlayerShips.AfterSelect += new TreeViewEventHandler(tvPlayerShips_NodeMouseClick);
                _playerShips.tvPlayerShips.AfterSelect += new TreeViewEventHandler(tvPlayerShips_NodeMouseClick);
            }

            foreach (TreeNode node in _playerShips.tvPlayerShips.Nodes)
            {
                LoadContextMenuIntoShipTreeNodes(node);
            }

            SetReadyText(); 
        }



        void tvPlayerShips_NodeMouseClick(object sender, TreeViewEventArgs e)
        {
            if(e.Node.Tag is iWeapon
                && _playerShips.SelectedShip != null)
            {
                if (_playerShips.SelectedShip.CurrentMovementOrder == null)
                {
                    _mapControl.DrawFireArc(_playerShips.SelectedShip,
                                            new LogicalPoint(_playerShips.SelectedShip.PositionX,
                                                             _playerShips.SelectedShip.PositionY), (iWeapon)e.Node.Tag,
                                            _playerShips.SelectedShip.Heading);
                }
                else
                {
                    _mapControl.DrawFireArc(_playerShips.SelectedShip,
                                            new LogicalPoint(_playerShips.SelectedShip.PositionXAfterMoveOrder,
                                                             _playerShips.SelectedShip.PositionYAfterMoveOrder), (iWeapon)e.Node.Tag,
                                            _playerShips.SelectedShip.HeadingAfterMoveOrder);
                }
                

            }
            else if (_playerShips.SelectedShip != null)
            {
                _mapControl.UpdateMap(_playerShips.SelectedShip);
            }
        }

       
        private void AssignMovement_Load(object sender, EventArgs e)
        {
            if(_currentGame != null
                && _currentGame.CurrentState is MovementState)
            {
                foreach(Player player in _currentGame.Players)
                {
                    cbCurrentPlayer.Items.Add(player.PlayerType);
                }

                if (cbCurrentPlayer.Items.Count > 0)
                {
                    cbCurrentPlayer.SelectedIndex = 0;
                }

                _playerShips.tvPlayerShips.ShowNodeToolTips = true;
                foreach (TreeNode node in _playerShips.tvPlayerShips.Nodes)
                {
                    LoadContextMenuIntoShipTreeNodes(node);
                }

                
            }

            SetReadyText();
        }




        private void btnMoveState_Click(object sender, EventArgs e)
        {
            _currentGame.Players[(int)((TypeOfPlayer)cbCurrentPlayer.SelectedItem)].MovementOrdersCommited = !_currentGame.Players[(int)((TypeOfPlayer)cbCurrentPlayer.SelectedItem)].MovementOrdersCommited;

            SetReadyText();

            bool allCommit = true;

            foreach (Player player in _currentGame.Players)
            {
                if (!player.MovementOrdersCommited)
                {
                    allCommit = false;
                }
            }

            if (allCommit)
            {
                _currentGame.CurrentState.AdvanceState();
            }
        }

        private void SetReadyText()
        {
            string readyText =
                _currentGame.Players[(int) ((TypeOfPlayer) cbCurrentPlayer.SelectedItem)].MovementOrdersCommited
                    ? "ready"
                    : "not ready";

            btnMoveState.Text = string.Format(@"{0} is {1} to move to attack phase",
                                              ((TypeOfPlayer) cbCurrentPlayer.SelectedItem).ToString(), readyText);
        }
    }
}
